package com.mes.bac.backend.controller;

import com.mes.bac.backend.common.vo.R;
import com.mes.bac.backend.dto.ReorderItemDTO;
import com.mes.bac.backend.service.IDispatchService;
import com.mes.bac.backend.vo.PieceLifecycleVO;
import com.mes.bac.backend.vo.ScheduleResultVO;
import com.mes.bac.backend.vo.WorkstationQueueVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;

@Tag(name = "5. 调度与执行", description = "工件调度、状态更新及队列查询")
@RestController
@RequestMapping("/api/dispatch")
public class DispatchController {

    @Autowired
    private IDispatchService dispatchService;

    @Operation(summary = "更新工位队列顺序")
    @PutMapping("/queue/reorder")
    public R<String> reorderQueue(@RequestBody List<ReorderItemDTO> reorderItems) {
        dispatchService.reorderQueue(reorderItems);
        return R.successMessage("队列顺序已更新");
    }

    @Operation(summary = "暂停工件加工")
    @PutMapping("/{dispatchId}/pause")
    public R<String> pause(@PathVariable Long dispatchId) {
        dispatchService.pauseProcessing(dispatchId);
        return R.successMessage("工件已暂停");
    }

    @Operation(summary = "继续工件加工")
    @PutMapping("/{dispatchId}/resume")
    public R<String> resume(@PathVariable Long dispatchId) {
        dispatchService.resumeProcessing(dispatchId);
        return R.successMessage("工件已恢复加工");
    }

    @Operation(summary = "[核心] 触发一次全局智能调度")
    @PostMapping("/schedule")
    public R<List<ScheduleResultVO>> schedule() {
        List<ScheduleResultVO> results = dispatchService.schedulePieces();
        String message = results.isEmpty() ? "没有需要调度的新工件" : "调度任务已成功触发";
        return R.success(message, results);
    }

    @Operation(summary = "启动工件加工")
    @PutMapping("/{dispatchId}/start")
    public R<String> start(@PathVariable Long dispatchId) {
        dispatchService.startProcessing(dispatchId);
        return R.successMessage("工件已启动加工");
    }

    @Operation(summary = "完成工件加工")
    @PutMapping("/{dispatchId}/complete")
    public R<ScheduleResultVO> complete(@PathVariable Long dispatchId) {
        Optional<ScheduleResultVO> nextStop = dispatchService.completeProcessing(dispatchId);
        return R.success("工件已完成加工", nextStop.orElse(null));
    }

    @Operation(summary = "[核心] 获取工位实时队列")
    @GetMapping("/queue/{wsid}")
    public R<List<WorkstationQueueVO>> getQueue(@PathVariable String wsid) {
        return R.success(dispatchService.getQueueByWorkstationId(wsid));
    }

    /**
     * 【新增的API】
     */
    @Operation(summary = "获取单个工件的生命周期轨迹")
    @GetMapping("/lifecycle/{pieceId}")
    public R<List<PieceLifecycleVO>> getLifecycle(@PathVariable String pieceId) {
        return R.success(dispatchService.getPieceLifecycle(pieceId));
    }

    @Operation(summary = "获取所有正在进行中的任务（全局任务池）")
    @GetMapping("/active-tasks")
    public R<List<WorkstationQueueVO>> getActiveTasks() {
        return R.success(dispatchService.getQueueByWorkstationId(null)); // 复用查询，传入null表示查询全部
    }
}